package com.ruoyi.fjtseac.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.fjtseac.config.OpenIdConfig;
import com.ruoyi.fjtseac.controller.UserController;
import com.ruoyi.fjtseac.domain.dto.UserDto;
import com.ruoyi.fjtseac.domain.po.User;
import com.ruoyi.fjtseac.mapper.UserMapper;
import com.ruoyi.fjtseac.service.UserService;
import com.ruoyi.fjtseac.util.DtoToPoMapper;
import com.ruoyi.fjtseac.util.GetPostUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-29
 */
@Service
public class UserServiceImpl implements UserService
{

    static java.util.logging.Logger log = java.util.logging.Logger.getLogger(String.valueOf(UserServiceImpl.class));

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OpenIdConfig openIdConfig;

    /**
     * 查询用户
     * 
     * @param account 用户主键
     * @return 用户
     */
    @Override
    public User selectUserByAccount(Long account)
    {
        return userMapper.selectUserByAccount(account);
    }

    /**
     * 查询用户列表
     * 
     * @param userDto 用户
     * @return 用户
     */
    @Override
    public List<User> selectUserList(UserDto userDto)
    {
        User user = DtoToPoMapper.INSTANCE.toUser(userDto);
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户
     * 
     * @param userDto 用户
     * @return 结果
     */
    @Override
    public int insertUser(UserDto userDto)
    {
        User user = DtoToPoMapper.INSTANCE.toUser(userDto);
        return userMapper.insertUser(user);
    }

    /**
     * 修改用户
     * 
     * @param userDto 用户
     * @return 结果
     */
    @Override
    public int updateUser(UserDto userDto)
    {
        User user = DtoToPoMapper.INSTANCE.toUser(userDto);
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除用户
     * 
     * @param accounts 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteUserByAccounts(Long[] accounts)
    {
        return userMapper.deleteUserByAccounts(accounts);
    }

    /**
     * 删除用户信息
     * 
     * @param account 用户主键
     * @return 结果
     */
    @Override
    public int deleteUserByAccount(Long account)
    {
        return userMapper.deleteUserByAccount(account);
    }

    @Override
    public AjaxResult getOpenId(String code) {
        String msg = MakeOpenId(code);
        if(msg.equals("false")){
            return AjaxResult.error("授权码无效");
        }

        return AjaxResult.success(msg);
    }

    public String MakeOpenId(String code) {
        String appid = openIdConfig.getAppId();
        String key = openIdConfig.getAppSecret();
        String openId = null;
        String resultOpen = GetPostUntil.sendGet("https://api.weixin.qq.com/sns/jscode2session",
                "appid=" + appid +//小程序ID
                        "&secret="+ key + //小程序密钥
                        "&js_code="+ code + //前端 传入 code
                        "&grant_type=authorization_code");

        JSONObject jsonObject = JSONObject.parseObject(resultOpen);
        if (jsonObject.containsKey("errcode")) {
            log.info("code无效，openId获取失败——小程序获取");
        }
        if (jsonObject.get("openid") != null)
            openId = jsonObject.get("openid").toString();

        else return "false";

        return openId;
    }
}
